#include "mincut.h"
#include <icache/footage.h>
#include <imglib/pyramid.h>

namespace trm {

namespace mincut {

/// The purpose of mincut tracker is to prepare everything to
/// build big & complex graph, and segment it in the solve(...) method.
/// tracker allowes a user to set obligatory foreground & background masks for any frame
class tracker {
public:
	void init(size_extent const & dim, uint duration, params const & p, bool overbuild_obligatory_masks);
	size_extent const & dim() const
		{ return dim_; }
	uint duration() const
		{ return masks_.size(); }
	void set_obligatory_foreground_pixels(uint frame, mask m);
	void set_obligatory_background_pixels(uint frame, mask m);
	/// same effect as set_obligatory_foreground_pixels(frame, m) and set_obligatory_background_pixels(frame, ~m)
	void set_exact_mask(uint frame, mask const & m);
	void solve(icache::footage_sptr const & footage, uint start_frame,
		uint median_len, ///< how many masks to pass into median filter in a time
		std::string const & out_foremasks_pattern, 
		std::string const & out_backmasks_pattern,
		std::string const & out_graymasks_pattern,
		std::string const & out_alphaimgs_pattern);
private:
	/// operates with diminished (on given scale) copies of frames from footage
	void solve_core(icache::footage_sptr const & footage, uint start_frame,
		uint level,
		std::vector< img::pyramid<mask> > & fmask_pyramid,  ///< reads and writes level #level
		std::vector< img::pyramid<mask> > & bmask_pyramid,
		bool need_graymasks);
	void load_image(icache::footage_sptr const & footage, uint frame, uint scale,
		byte_image & im) const;
private:
	struct obligatory_masks {
		mask foreground;
		mask background;
	};
private:
	size_extent dim_;
	std::vector<obligatory_masks> masks_;
	std::vector<gray_mask> gray_masks_;
	params params_;
	bool overbuild_obligatory_masks_;
	uint lowlevel_max_motion_;
};

} //mincut

} //param
